package com.sun.module_home.ui.index

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import com.sun.module_base.app.BaseViewModel
import com.sun.module_base.app.ResultState
import com.sun.module_common.model.Article
import com.sun.module_home.model.BannerBean
import com.sun.module_common.model.ListWrapper
import com.sun.module_home.net.HttpRepository
import com.sun.module_network.HttpResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import javax.inject.Inject

/**
 *@author:sunc
 *@time :2025/8/1 16:00
 *@desc: 问答
 */
@HiltViewModel
class IndexViewModel @Inject constructor(private val httpRepository: HttpRepository) :
    BaseViewModel() {

    private val _articleFlow =
        MutableStateFlow<ArticleState>(ResultState.Loading())
    val articleFlow = _articleFlow.asStateFlow()

    private val _topArticleFlow =
        MutableStateFlow<TopArticleState>(ResultState.Loading())
    val topArticleFlow = _topArticleFlow.asStateFlow()

    private val _bannerFlow =
        MutableStateFlow<BannerState>(ResultState.Loading())
    val bannerFlow = _bannerFlow.asStateFlow()

    var page by mutableIntStateOf(0)

    private val _isRefresh = mutableStateOf(false)
    val isRefresh: Boolean get() = _isRefresh.value

    private val _isLoadMore = mutableStateOf(false)
    val isLoadMore: Boolean get() = _isLoadMore.value


    init {
        getBanners()
        refresh()
//        getTopArticleList()
    }

//    fun getArticleList() {
//        viewModelScope.launch {
//            val flow = flow {
//                if (isRefresh) {
//                    val response = withTimeout(30 * 1000) { httpRepository.getTopArticle() }
////                    throw RuntimeException("模拟异常")
//                    emit(response)
//                }
//            }
//            val flow2 = flow {
//                val response = withTimeout(30 * 1000) { httpRepository.getIndexList(page) }
////                throw RuntimeException("模拟异常2")
//                emit(response)
//            }
//            val flow3 = combine(flow, flow2) { list1, list2 ->
//                list1.getResponseData()?.let {
//                    if (!isLoadMore) {
//                        list2.getResponseData()?.datas?.addAll(it)
//                    }
//                }
//                list2
//            }
//
//            launchUIbyFlow(flow = flow3, _articleFlow, onCompletion = {
//                _isRefresh.value = false
//                _isLoadMore.value = false
//            })
//
//        }
//    }
    fun getArticleList() {
        launchUI({ httpRepository.getIndexList(page) }, _articleFlow, onCompletion = {
            _isRefresh.value = false
            _isLoadMore.value = false
        })
    }

    fun getTopArticleList() {
        launchUI({ httpRepository.getTopArticle() }, _topArticleFlow, onCompletion = {

        })
    }

    fun getBanners() {
        launchUI({ httpRepository.getBanners() }, _bannerFlow, onCompletion = {
        })
    }

    fun refresh() {
        _isRefresh.value = true
        page = 0
        getTopArticleList()
        getArticleList()
    }


    fun loadMore() {
        _isLoadMore.value = true
        page++
        getArticleList()
    }

}
//包不同，允许取相同名字
typealias ArticleState = ResultState<ListWrapper<Article>?>
typealias BannerState = ResultState<MutableList<BannerBean>?>
typealias TopArticleState = ResultState<MutableList<Article>?>